package com.litong.rms.util;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.litong.rms.bean.Auth;
import com.litong.rms.bean.Function;
import com.litong.rms.bean.Module;
import com.litong.rms.bean.RoleAuth;
import com.litong.rms.bean.User;
import com.litong.rms.bean.UserRole;
import com.litong.rms.service.AuthService;
import com.litong.rms.service.FunctionService;
import com.litong.rms.service.ModuleService;
import com.litong.rms.service.RoleAuthService;
import com.litong.rms.service.UserRoleService;

public class AuthUtil {
	static BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
	
	
	public static List<Auth> getAuthList(){	
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		UserRoleService userRoleService = (UserRoleService)bf.getBean("userRoleService");
		RoleAuthService roleAuthSerivce = (RoleAuthService)bf.getBean("roleAuthService");
		AuthService authSerivce = (AuthService)bf.getBean("authService");
		
		List<UserRole> urList = userRoleService.getUserRoleList(user.getUserId());
		List<RoleAuth> totalRoleList =  new ArrayList<RoleAuth>();
		List<RoleAuth> raList = new ArrayList<RoleAuth>();
		for(int i=0;i<urList.size();i++){
			raList =  roleAuthSerivce.getRoleAuthList(urList.get(i).getRoleId());
			if((!raList.isEmpty())&&raList!=null){
				for(int j=0;j<raList.size();j++){
					totalRoleList.add(raList.get(j));
				}
			}	
		}
		//拿到权限List
		List<Auth> authList = new ArrayList<Auth>();
		for(int i=0;i<totalRoleList.size();i++){
			if(authSerivce.getAuthById(totalRoleList.get(i).getAuthId())!=null){
				authList.add(authSerivce.getAuthById(totalRoleList.get(i).getAuthId()));
			}
			
		}
		
		return authList;
		
	}
	
	public static List<Module> getModuleList(User user){
		ModuleService moduleSerivce = (ModuleService)bf.getBean("moduleService");
		//拿到模块
		List<Module> moduleList = new ArrayList<Module>();
		List<Auth> authList =getAuthList();
		List<Integer> tempList = new ArrayList<Integer>();
		
		if(authList==null||authList.isEmpty()){
			return null;
		}
		for(int i=0;i<authList.size();i++){
			if(!tempList.contains(authList.get(i).getModuleId())){//过滤掉重复模块
				moduleList.add(moduleSerivce.findById(authList.get(i).getModuleId()));
				tempList.add(authList.get(i).getModuleId());//将模块添加到已有模块id列表为过滤做准备
			}	
		}
		//将模块
		/*Set<String> moduleNameSet = new HashSet<String>();
		for(int i=0;i<moduleList.size();i++){
			moduleNameSet.add(moduleList.get(i).getModuleName());
		}
		System.out.println(moduleNameSet);*/
		return moduleList;
	}
	
	//拿到模板所对应的功能
	public static List<Function> getFunctionList(Module module){
		FunctionService functionService = (FunctionService)bf.getBean("functionService");
		List<Function> functionList = new ArrayList<Function>();
		List<Integer> tempList = new ArrayList<Integer>();
		List<Auth> authList =getAuthList();
		for(int i=0;i<authList.size();i++){
			if(authList.get(i).getModuleId()==module.getModuleId()){
				if(!tempList.contains(authList.get(i).getFunctionId())){//过滤掉重复功能
					functionList.add(functionService.findById(authList.get(i).getFunctionId()));
					tempList.add(authList.get(i).getFunctionId());//将模块添加到已有功能id列表为过滤做准备
				}
				
			}
		}
		return functionList;
	}
}
